package org.michael.u.brg.i18n.litz;

import java.awt.Font;
import java.io.IOException;
import java.net.URI;
import java.util.*;
import javax.xml.parsers.*;

import org.xml.sax.*;

public class KeyMap {

    private final Font font;
    private final Set<KeyPunch> keyPunches;
    private final String name;

    public KeyMap(String nameToUse, Font fontToUse, Set<KeyPunch> keyPunchesToUse) {
        name = nameToUse;
        font = fontToUse;
        keyPunches = keyPunchesToUse;
    }

    public Font getFont() {
        return font;
    }

    public Set<KeyPunch> getKeyPunches() {
        return keyPunches;
    }

    public String getName() {
        return name;
    }

    public static KeyMap readFromXmlFile(URI fileUri)
            throws SAXException, ParserConfigurationException, IOException {
        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        saxFactory.setValidating(true);
        SAXParser parser = saxFactory.newSAXParser();
        KeyMapSaxHandler handler = new KeyMapSaxHandler(fileUri);
        parser.parse(fileUri.toString(), handler);
        return new KeyMap(handler.getName(), handler.getFont(), handler.getKeyPunches());
    }

    @Override public String toString() {
        return String.format("Name = %s, font = %s, keys={%s}", name, font, keyPunches);
    }
}
